package cn.com.suiyi.shixingshengxian.commoditydetail;

import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.com.suiyi.shixingshengxian.R;
import cn.com.suiyi.shixingshengxian.base.IBasePresenter;
import cn.com.suiyi.shixingshengxian.base.MVPActivity;
import cn.com.suiyi.shixingshengxian.commoditydetail.bean.Commodity;
import cn.com.suiyi.shixingshengxian.commoditydetail.bean.CommodityPic;
import cn.com.suiyi.shixingshengxian.commoditydetail.myview.TBLayout;

/**
 * Created by hansen on 2016/7/22.
 */
public class CommodityActivity extends MVPActivity implements CommodityContract.ComView {
    @InjectView(R.id.commodity_last_time_info)
    TextView commodityLastTimeInfo;
    @InjectView(R.id.rate_price)
    TextView ratePrice;
    @InjectView(R.id.rate_weight)
    TextView rateWeight;
    @InjectView(R.id.sx_price_name)
    TextView sxPriceName;
    @InjectView(R.id.sx_price)
    TextView sxPrice;
    @InjectView(R.id.sp_weight)
    TextView spWeight;
    @InjectView(R.id.bian)
    TextView bian;
    @InjectView(R.id.commodity_number)
    TextView commodityNumber;
    @InjectView(R.id.header)
    LinearLayout header;
    @InjectView(R.id.commodity_pic)
    ImageView commodityPics;
    @InjectView(R.id.footer)
    LinearLayout footer;
    @InjectView(R.id.tblayout)
    TBLayout tblayout;
    @InjectView(R.id.commodity_banner)
    Banner commodityBanner;
    private String path;
    private List<Commodity.DataBean.BannerBean> picList=new ArrayList<>();
    @InjectView(R.id.commodity_title)
    TextView commodityTitle;


    @Override
    protected void initData() {

    }

    @Override
    public int getLayout() {
        return R.layout.activity_commodity;
    }

    @Override
    public void initView() {
        commodityBanner.setBannerStyle(Banner.INVISIBLE);
        commodityBanner.setIndicatorGravity(Banner.CENTER);
        commodityBanner.isAutoPlay(false);

    }


    @Override
    public IBasePresenter getPresenter() {
        return new CommodityPresenter("53");
    }

    @Override
    public void onCommodity(Commodity commodity) {

        picList.addAll(commodity.getData().getBanner());
        Log.i("tag","---->"+commodity.getData().getBanner().get(0).getImage());
        List<String> list=new ArrayList<>();

        for (int i=0;i<picList.size();i++){
            list.add(picList.get(i).getImage());

        }
        commodityBanner.setImages(list, new Banner.OnLoadImageListener() {
            @Override
            public void OnLoadImage(ImageView view, Object url) {
                Glide.with(getApplicationContext()).load(url).into(view);
            }
        });

        commodityTitle.setText(commodity.getData().getProduct().getName());
        commodityLastTimeInfo.setText(commodity.getData().getProduct().getLastTimeInfo());
        ratePrice.setText(commodity.getData().getProduct().getRatePrice());
        rateWeight.setText(commodity.getData().getProduct().getRateWeight());
        sxPrice.setText(commodity.getData().getProduct().getSxPrice());
        spWeight.setText(commodity.getData().getProduct().getSpWeight());
        commodityNumber.setText(commodity.getData().getProduct().getNumber());
    }

    @Override
    public void onCommodityPic(CommodityPic commodityPic) {
        //通过正则表达式获取图片地址
        String pic = commodityPic.getData().getDesc();
        Pattern p = Pattern.compile("<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>");//<img[^<>]*src=[\'\"]([0-9A-Za-z.\\/]*)[\'\"].(.*?)>");
        Matcher m = p.matcher(pic);
        List<String> listPic = new ArrayList<>();
        while (m.find()) {
            listPic.add(m.group(1));
        }

        Glide.with(this).load(listPic.get(0)).into(commodityPics);
    }

    @Override
    public void onFail(String str) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.inject(this);
    }


}

